define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config) {

            // 流程
            $scope.phases = [
                'process_feeder',
                'process_material'
            ];

            $scope.init = function(){
                $scope.user_name = config.cache.name;
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                // 預設流程狀態
                $scope.phases_index = 0;
                $scope.disable_control = config.cache.sys_parameter["SMTInfo.SMTPartDisableControl"] //禁用物料卡控
            
                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            };

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            };

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if($scope.phases_index == 0){
                        $scope.feeder_no = '';
                        $scope.machine_no = '';
                        $scope.slot_no = '';
                        $scope.origin_material_no = '';
                        $scope.origin_material_product_no = '';
                        $scope.origin_material_item_spec = '';
                        $scope.origin_reel_no ='';
                        $scope.actual_qty = '';
                    }
                    $scope.new_reel_no = '';
                    $scope.new_material_no = '';
                    $scope.new_material_product_no = '';
                    $scope.new_material_item_spec = '';
                    $scope.new_material_qty = '';
                    $scope.new_material_actual_qty = '';
                };
            };

            function reel_process(itemList){//禁用物料卡控結束後執行物料流程
                $scope.phases_index = 2;
                $scope.new_reel_no = itemList[0].reel_no
                $scope.new_material_no = itemList[0].material_no;
                $scope.new_material_product_no = itemList[0].product_no;
                $scope.new_material_item_spec = itemList[0].item_spec;
                $scope.new_material_qty = itemList[0].qty;
                $scope.new_material_actual_qty =  parseFloat(itemList[0].actual_qty)+parseFloat($scope.actual_qty);
            }

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // Feeder流程
                    if($scope.phases_index == 0){
                        var content = {
                            feeder_no: barcode
                        };
                        feeder_info_get(content,function(data){
                            // 判斷 feeder是否在已上線狀態。 P.S.有綁定設備才算是已上線
                            if(data){
                                // 60 已上線
                                if(data.state == '60'){
                                    $scope.phases_index = 1;
    
                                    $scope.mode = data.mode;
                                    $scope.plot_no = data.plot_no;
                                    $scope.machine_no = data.equipment_no;
                                    $scope.mfu_no = data.mfu_no;
                                    $scope.feederlist_no = data.feederlist_no;
                                    $scope.feederlist_ver = data.feederlist_ver;
    
                                    $scope.feeder_type_no = data.feeder_type_no;
                                    $scope.feeder_no = data.feeder_no;
                                    $scope.slot_no = data.slot_no;
    
                                    $scope.origin_reel_no = data.reel_no;
                                    $scope.origin_material_no = data.material_no;
                                    $scope.origin_material_product_no = data.product_no;
                                    $scope.origin_material_item_spec = data.item_spec;
                                    $scope.actual_qty = data.actual_qty;
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT05.msg.feeder_no_online'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 物料流程
                    } else if($scope.phases_index == 1){
                        material_info_get(barcode,function(itemList){
                            if(itemList && itemList.length > 0){
                                if((+itemList.remain_time) <= 0 && itemList.remain_time!='-1'){
                                    $scope.$root.showAlert($filter('translate')('SMT05.msg.MSD_over_time'));
                                }else{
                                    if($scope.disable_control == '1'){//啟動禁用物料
                                        if(itemList[0].forbidden_reel == '1'){//禁用物料
                                            $scope.$root.showAlert($filter('translate')('fordden_msg.forbid_reel'));
                                        }else{
                                            // MSD卡控卡這裡
                                        if((+itemList[0].remain_time) < 0 && itemList[0].remain_time !='-0.1'){
                                            $('#toast-panel').css("background-color","rgb(219, 75, 60)");
                                            $scope.$root.showAlert($filter('translate')('SMT03.msg.MSD_over_time'));
                                            return;
                                        }else if((+itemList[0].remain_time) <= 30 && (+itemList[0].remain_time) > 0){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_in_a_hurry'));
                                            return
                                        }else if((+itemList[0].remain_time) <= 60 && (+itemList[0].remain_time) > 30){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_go_to_over_time'));
                                            return
                                        }else{
                                            reel_process(itemList);
                                        }
                                        }
                                    }else{
                                        // MSD卡控卡這裡
                                        if((+itemList[0].remain_time) < 0 && itemList[0].remain_time !='-0.1'){
                                            $('#toast-panel').css("background-color","rgb(219, 75, 60)");
                                            $scope.$root.showAlert($filter('translate')('SMT03.msg.MSD_over_time'));
                                            return;
                                        }else if((+itemList[0].remain_time) <= 30 && (+itemList[0].remain_time) > 0){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_in_a_hurry'));
                                            return
                                        }else if((+itemList[0].remain_time) <= 60 && (+itemList[0].remain_time) > 30){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_go_to_over_time'));
                                            return
                                        }else{
                                            reel_process(itemList);
                                        }
                                    } 
                                }
                            } else {
                                $scope.$root.showAlert($filter('translate')('SMT05.msg.feeding_comparison_incorrect_please_reconfirm'));
                            };
                        });
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                    };
                };
            };

            // 送出資料
            $scope.confirm = function(){
                if($scope.phases_index == 2){
                    var content = {
                        feeder_type_no: $scope.feeder_type_no,
                        feeder_no: $scope.feeder_no,
                        feeder_mode: $scope.mode,
                        equipment_no: $scope.machine_no,
                        mfu_no: $scope.mfu_no,
                        plot_no: $scope.plot_no,
                        feederlist_no: $scope.feederlist_no,
                        feederlist_ver: $scope.feederlist_ver,
                        slot_no: $scope.slot_no,
                        material_no: $scope.origin_material_no,
                        reel_no: $scope.origin_reel_no,
                        actual_qty: $scope.actual_qty,
                        /*
                        new_feeder_type_no: $scope.actual_qty,
                        new_feeder_no: $scope.actual_qty,
                        new_feeder_state: $scope.actual_qty,
                        new_slot_no: $scope.new_slot_no,
                        */
                        new_material_no: $scope.new_material_no,
                        new_reel_no: $scope.new_reel_no,
                        qty: $scope.new_material_actual_qty
                    };
                    feeder_reel_change_set_process(content,function(data){
                        $scope.BackToPhase(0);
                        $scope.$root.showToast($filter('translate')('SMT05.msg.success'));
                    });
                };
            };

            // 帶出 Feeder資訊
            function feeder_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 取得 捲料資料
            function material_info_get(reel_no, callback){
                let content = {
                    reel_no:reel_no,
                }
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.reel_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 回寫 續料資訊
            function feeder_reel_change_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_reel_change_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
